package com.minelx.datamicroscope.core.condition;

import com.alibaba.fastjson.JSONObject;
import com.minelx.datamicroscope.core.values.CommonField;
import io.gitee.minelx.commontools.factor.CloseableIterator;
import io.gitee.minelx.commontools.factor.Factor;

import java.util.List;

class RawInValuesCondition implements RawCondition {

    private final CommonField singleKey;

    private final List<String> values;

    RawInValuesCondition(CommonField singleKey, List<String> values) {
        this.singleKey = singleKey;
        this.values = values;
    }

    @Override
    public Factor<Condition> toFactor() {
        return factorFactory().factor();
    }

    public ConditionFactorFactory factorFactory() {
        return new SingleKeyStreamSourceFactorFactory(
                singleKey,
                // memory iterator don't need to close
                () -> CloseableIterator.fromIterator(values.iterator()));
    }

    @Override
    public JSONObject toJSON() {
        return RawCondition.sticky(KeyOperandCondition.of(singleKey, Operand.in(values))).toJSON();
    }
}
